import 'dart:ui';

import 'package:demo/model/person_model.dart';
import 'package:demo/theme/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

import '../../../../model/constant_model.dart';
import '../../../../model/order_model.dart';
import '../../../../net/api_provider.dart';
import '../../../routes/app_pages.dart';

class OrderDetailController extends GetxController {

  var titleBgColor =  Color_main.obs;
  var titleStr =  '订单详情'.obs;
  var hint1Str =  ''.obs;
  var hint2Str =  ''.obs;
  var hintBtn = true.obs;
  var hintBtnStr = ''.obs;
  var hintBtnColor =  Color_main.obs;

  var applyMoney = '申请金额：¥--'.obs;
  var applyTerm = '贷款期限：--'.obs;
  var userName = ''.obs;
  var userPhone = ''.obs;
  var orderLoanNum = ''.obs;
  var orderLoanTime = ''.obs;

  var loanNum = ''; // 订单编号
  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    loanNum = Get.arguments['loan_num'];

    getLoanDetail(loanNum);
    getPersonDetail();
  }

  ApiProvider apiProvider = Get.find();
  void getLoanDetail(String loanNum) async{
    var constantRp = await apiProvider.getData({}, ApiProvider.constant);
    var detailRp = await apiProvider.getData({'loan_num': loanNum}, ApiProvider.loan_detail);

    if(constantRp.code == 200 && detailRp.code == 200){
      var orderDetail = Order.fromJson(detailRp.data);
      orderStatus = orderDetail.status ?? -1;
      doOrderStatus();
      applyMoney.value = '申请金额：¥${orderDetail.loanAmount}';
      var constant = Constant.fromJson(constantRp.data);
      var timeList = constant.timeList;
      timeList?.forEach((key, value) {
        if(int.parse(key) == orderDetail.loanPeriodId){
          applyTerm.value = '贷款期限：$value';
        }
      });

      var time = DateTime.parse('${orderDetail.createdAt}');
      orderLoanTime.value = '${time.year}-${time.month.toString().padLeft(2, '0')}-${time.day.toString().padLeft(2, '0')} ${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}:${time.second.toString().padLeft(2, '0')}';
      orderLoanNum.value = '${orderDetail.loanNum}';
    }else{
      EasyLoading.showToast('${constantRp.msg}，${detailRp.msg}');
    }
  }
  // 获取姓名和手机号
  void getPersonDetail() async{
    var detail = await apiProvider.getData({}, ApiProvider.person_detail);
    if(detail.code == 200){
      var person = Person.fromJson(detail.data);
      userName.value = person.name;
      userPhone.value = person.phone;
    }else{
      EasyLoading.showToast('${detail.msg}');
    }
  }

  var dot1Color = Color_green.obs;
  var dot2Color = Color_green.obs;
  var dot3Color = Color_green.obs;
  var dot4Color = Color_green.obs;
  var dot5Color = Color_green.obs;
  var line1Color = Color_green.obs;
  var line2Color = Color_green.obs;
  var line3Color = Color_green.obs;
  var line4Color = Color_green.obs;
  // 处理很多东西
  void doOrderStatus(){
      switch (orderStatus) {
        case 3: // 审核中
          titleBgColor.value =  Color_main;
          titleStr.value = '审核中';
          hint1Str.value = '审核中';
          hint2Str.value = '审批流程进行中，请耐心等待';
          dot1Color.value = Color_131732;
          dot2Color.value = Color_green;
          dot3Color.value = Color_green;
          dot4Color.value = Color_green;
          dot5Color.value = Color_green;
          line1Color.value = Color_131732;
          line2Color.value = Color_green;
          line3Color.value = Color_green;
          line4Color.value = Color_green;
          hintBtn.value = true;
          break;
        case 5: // 审核拒绝
          titleBgColor.value =  Color_red;
          titleStr.value = '审核未通过';
          hint1Str.value = '审核未通过';
          hint2Str.value = '您的订单审核未通过';
          dot1Color.value = Color_131732;
          dot2Color.value = Color_red;
          dot3Color.value = Color_red;
          dot4Color.value = Color_red;
          dot5Color.value = Color_red;
          line1Color.value = Color_131732;
          line2Color.value = Color_red;
          line3Color.value = Color_red;
          line4Color.value = Color_red;
          hintBtn.value = false;
          hintBtnStr.value = '查看其他产品';
          hintBtnColor.value = Color_red;
        break;
      case 4 : // 审核通过
        titleBgColor.value =  Color_main;
        titleStr.value = '审核通过';
        hint1Str.value = '审核通过';
        hint2Str.value = '您的订单已审核通过，请尽快签约';
        dot1Color.value = Color_131732;
        dot2Color.value = Color_131732;
        dot3Color.value = Color_green;
        dot4Color.value = Color_green;
        dot5Color.value = Color_green;
        line1Color.value = Color_131732;
        line2Color.value = Color_131732;
        line3Color.value = Color_green;
        line4Color.value = Color_green;
        hintBtn.value = false;
        hintBtnStr.value = '去借钱';
        hintBtnColor.value = Color_main;
        break;
      case 6 : // 下单成功，待放款(该状态不会回调，下单成功后默认就是该状态
        titleBgColor.value =  Color_red;
        titleStr.value = '放款中';
        hint1Str.value = '放款中';
        hint2Str.value = '您的贷款正在放款，请耐心等待';
        dot1Color.value = Color_131732;
        dot2Color.value = Color_131732;
        dot3Color.value = Color_131732;
        dot4Color.value = Color_131732;
        dot5Color.value = Color_green;
        line1Color.value = Color_131732;
        line2Color.value = Color_131732;
        line3Color.value = Color_131732;
        line4Color.value = Color_green;
        hintBtn.value = true;
        break;
      case 8: // 放款失败
        titleBgColor.value =  Color_red;
        titleStr.value = '放款失败';
        hint1Str.value = '放款失败';
        hint2Str.value = '您的借款放款失败，请重新申请';
        dot1Color.value = Color_131732;
        dot2Color.value = Color_131732;
        dot3Color.value = Color_131732;
        dot4Color.value = Color_red;
        dot5Color.value = Color_red;
        line1Color.value = Color_131732;
        line2Color.value = Color_131732;
        line3Color.value = Color_131732;
        line4Color.value = Color_red;
        hintBtn.value = false;
        hintBtnStr.value = '请您重新借款';
        hintBtnColor.value = Color_red;
        break;
      case 9 : // 已放款
        titleBgColor.value =  Color_main;
        titleStr.value = '还款中';
        hint1Str.value = '还款中';
        hint2Str.value = '您的借款正在还款中，请正常使用';
        dot1Color.value = Color_131732;
        dot2Color.value = Color_131732;
        dot3Color.value = Color_131732;
        dot4Color.value = Color_131732;
        dot5Color.value = Color_green;
        line1Color.value = Color_131732;
        line2Color.value = Color_131732;
        line3Color.value = Color_131732;
        line4Color.value = Color_green;
        hintBtn.value = false;
        hintBtnStr.value = '去还款';
        hintBtnColor.value = Color_main;
        break;
      case 10 : // 逾期中
        titleBgColor.value =  Color_red;
        titleStr.value = '已逾期';
        hint1Str.value = '已逾期';
        hint2Str.value = '您的贷款已出现逾期，请及时还款';
        dot1Color.value = Color_131732;
        dot2Color.value = Color_131732;
        dot3Color.value = Color_131732;
        dot4Color.value = Color_131732;
        dot5Color.value = Color_red;
        line1Color.value = Color_131732;
        line2Color.value = Color_131732;
        line3Color.value = Color_131732;
        line4Color.value = Color_red;
        hintBtn.value = false;
        hintBtnStr.value = '请您尽快还款';
        hintBtnColor.value = Color_red;
        break;
      case 1: // 结清(仅在订单状态查询接口返回)
      case 2:
        titleBgColor.value =  Color_main;
        titleStr.value = '已结清';
        hint1Str.value = '已结清';
        hint2Str.value = '您已还清贷款，可继续申请';
        dot1Color.value = Color_131732;
        dot2Color.value = Color_131732;
        dot3Color.value = Color_131732;
        dot4Color.value = Color_131732;
        dot5Color.value = Color_131732;
        line1Color.value = Color_131732;
        line2Color.value = Color_131732;
        line3Color.value = Color_131732;
        line4Color.value = Color_131732;
        hintBtn.value = false;
        hintBtnStr.value = '继续借款';
        hintBtnColor.value = Color_main;
        break;
      default:
        titleBgColor.value =  Color_main;
        titleStr.value = '已过期';
        hint1Str.value = '已过期';
        hint2Str.value = '您的订单已过期，可重新申请';
        dot1Color.value = Color_131732;
        dot2Color.value = Color_131732;
        dot3Color.value = Color_131732;
        dot4Color.value = Color_131732;
        dot5Color.value = Color_131732;
        line1Color.value = Color_131732;
        line2Color.value = Color_131732;
        line3Color.value = Color_131732;
        line4Color.value = Color_131732;
        hintBtn.value = false;
        hintBtnStr.value = '重新借款';
        hintBtnColor.value = Color_main;
        break;
    }
  }

  var orderStatus = -1; // 订单状态
  void clickBtn(){
    switch (orderStatus) {
      case 3: // 审核中，按钮隐藏
        break;
      case 5: // 审核拒绝，去LoanAll
        Get.toNamed(Routes.PRODUCT_LIST,arguments: {'isLoadAll' : true});
        break;
      case 4 : // 审核通过，去借钱
        Get.toNamed(Routes.LOAN,arguments: {'loan_num' : loanNum});
        break;
      case 6 : // 下单成功，待放款，按钮隐藏
        break;
      case 8: // 放款失败，重新借款
        Get.offNamed(Routes.APPLY);
        break;
      case 9 : // 已放款，去还款
        // if (vm.orderDetail.value?.lcxz.equals("2")){ // 全流程
        Get.toNamed(Routes.REPAY_PLANS,arguments: {'loan_num' : loanNum});
        //   } else { // 半流程
        //     vm.getRepayUrl()
        //   }
        break;
      case 10 : // 逾期中，去还款
      // if (vm.orderDetail.value?.lcxz.equals("2")){ // 全流程
        Get.toNamed(Routes.REPAY_PLANS,arguments: {'loan_num' : loanNum});
        //   } else { // 半流程
        //     vm.getRepayUrl()
        //   }
        break;
      case 1: // 结清(仅在订单状态查询接口返回)，继续借款
      case 2:
        Get.offNamed(Routes.APPLY);
        break;
      default: // 过期重新借款
        Get.offNamed(Routes.APPLY);
        break;
    }
  }

  @override
  void onClose() {
    super.onClose();
  }

}
